package com.shaoshaohuo.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.ui.ReleaseActivity;
import com.shaoshaohuo.app.constant.AppConfig;


/**
 * Created by cao on 2016/1/5.
 */
public class CarParentFragment extends Fragment {

    private View content;
    private FrameLayout frame;
    private RadioGroup tab_menu;
    private RadioButton rb_release;

    private int checkId;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        content = inflater.inflate(R.layout.fragment_car_parent, null);
        initView();
        setUpView();
        return content;
    }

    private void initView() {
        frame = (FrameLayout) content.findViewById(R.id.frame);
        tab_menu = (RadioGroup) content.findViewById(R.id.tab_menu);
        rb_release = (RadioButton) content.findViewById(R.id.rb_release);
    }

    private void setUpView() {
        tab_menu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_main:
                        changeFragment(CarFragment.newInstance("argument"), checkedId);
                        break;
                    case R.id.rb_release:

                        break;
                    case R.id.rb_order:
                        changeFragment(CarOrderFragment.newInstance("argument", getActivity()), checkedId);
                        break;
                    case R.id.rb_my_info:
                        changeFragment(MyInfoFragment.newInstance("argument"), checkedId);
                        break;
                }
            }
        });
        tab_menu.check(R.id.rb_main);
        rb_release.setOnTouchListener(new View.OnTouchListener() {
            private boolean down  = false;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    down = true;
                }else if (event.getAction() == MotionEvent.ACTION_UP && down) {
                    getActivity().startActivity(new Intent(getActivity(), ReleaseActivity.class));
                    down = false;
                }else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                    down = false;
                }
                return true;
            }
        });
    }


    private void changeFragment(Fragment fragment, int checkedId) {
        checkId = checkedId;
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_OUT_CAR != AppConfig.FRAGMENT_NONE && AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_IN_CAR != AppConfig.FRAGMENT_NONE
                && AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_IN_CAR != AppConfig.FRAGMENT_NONE && AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_OUT_CAR != AppConfig.FRAGMENT_NONE) {
            transaction.setCustomAnimations(AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_IN_CAR, AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_OUT_CAR);
        } else {
            transaction.setTransition(AppConfig.FRAGMENT_TRANSITION);
        }
        transaction.replace(R.id.frame, fragment, fragment.getClass().getSimpleName()).commit();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getChildFragmentManager().findFragmentByTag(CarFragment.class.getSimpleName());
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
